package Main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Component.MyComboBox;
import Component.MyForm;
import Component.MyLabel;
import Component.MyPanel;
import Component.MyStateButton;
import Component.MyTextButton;
import Component.MyTextCheckBox;
import Global.ResourceManager;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 *
 * @author Huy
 */
public class GameCustomState extends GameState {

	static final int[] START_MONEY = new int[]{500, 150, 1000, 10000};
	static final int[] LIMIT = new int[]{Integer.MAX_VALUE, 50, 75, 100, 150, 200};
	public static final int PLAYER = 0;
	public static final int COMPUTER = 1;
	public static final int EMPTY = 2;
	final int MOVE_TIME = 8;
	final int CP_PANEL_SPEED = 50;
	final int N_PANEL_SPEED = 50;
	final int CM_PANEL_SPEED = 60;
	int time = MOVE_TIME;
	private int state = PLAYER;
	private final MyStateButton[] colorsButton;
	private final MyComboBox[] playersBox;
	private int[] players;
	private MyPanel custumPlayerPanel;
	private MyPanel navigationPanel;
	private MyPanel custumMapPanel;
	private int maxNPlayer = 6;
	private int nComTeams;
	private Color panelColor;
	MyComboBox cbMapSize;
	MyComboBox cbMapSelect;
	MyComboBox cbStartMoney;
	MyComboBox cbLimit;
	MyComboBox cbLevelSelect;
	MyTextCheckBox cAlowCheat;

	public GameCustomState(Game _game) {
		super(_game);
		panelColor = new Color(0xcc220066, true);
		colorsButton = new MyStateButton[maxNPlayer];
		playersBox = new MyComboBox[maxNPlayer];
		players = new int[maxNPlayer];

		form = new MyForm(ResourceManager.getImage("menu/background"), game.getWidth(), game.getHeight());

		initCustumPlayerPanel();
		initCustumMapPanel();
		initNavigationPanel();

	}

	private void initCustumPlayerPanel() {
		custumPlayerPanel = new MyPanel(300, 300, panelColor, -370, 50);
		form.add(custumPlayerPanel);
		nComTeams = 1;
		for (int i = 0; i < maxNPlayer; i++) {
			playersBox[i] = new MyComboBox(new String[]{"PLAYER", "COMPUTER", "--close--"}, 50, 50 + i * 30);
			playersBox[i].setDisableColor(Color.BLUE);
			if (i == 0) {
				playersBox[i].setEnable(false);
				players[i] = PLAYER;
			} else if (i == 1) {
				playersBox[i].setSelectedIndex(COMPUTER);
				players[i] = COMPUTER;
			} else if (i > 1) {
				playersBox[i].setSelectedIndex(EMPTY);
				players[i] = EMPTY;
			}
			custumPlayerPanel.add(playersBox[i]);

			colorsButton[i] = new MyStateButton(
					ResourceManager.getImage("menu/colorchooser"), 2, 3, 200, 50 + i * 30);
			colorsButton[i].setState(i);
			colorsButton[i].setEnable(false);
			custumPlayerPanel.add(colorsButton[i]);

		}
	}

	private void initNavigationPanel() {
		navigationPanel = new MyPanel(200, 110, panelColor, -340, 450);
		MyTextButton b_Back = new MyTextButton("Back", 50, 60);
		navigationPanel.add(b_Back);
		MyTextButton b_Start = new MyTextButton("Start Game", 50, 20);
		navigationPanel.add(b_Start);
		form.add(navigationPanel);

		for (final MyComboBox box : playersBox) {
			box.setMouseAdapter(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					ValidatePlayer(box);
				}
			});
		}

		b_Back.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				state = 2;
			}
		});
		b_Start.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {

				game.gameState = new GameLoadingState(game, getSetting());
			}
		});
	}

	public GameCustumSetting getSetting() {
		GameCustumSetting setting = new GameCustumSetting();
		setting.mapSize = cbMapSize.getSelectedIndex();
		setting.mapType = cbMapSelect.getSelectedIndex();
		setting.playerList = players;
		setting.nTeams = nComTeams;
		setting.startMoney = START_MONEY[cbStartMoney.getSelectedIndex()];
		setting.limit = LIMIT[cbLimit.getSelectedIndex()];
		setting.level = cbLevelSelect.getSelectedIndex();
		setting.allowCheat = cAlowCheat.isChecked();
		return setting;
	}

	private void initCustumMapPanel() {
		custumMapPanel = new MyPanel(400, 400, panelColor, 850, 50);
		form.add(custumMapPanel);


		MyLabel lLevel = new MyLabel("Difficulty", 50, 50);
		lLevel.setForeColor(Color.WHITE);
		cbLevelSelect = new MyComboBox(new String[]{"Noob", "Amateur", "Professional"}, 200, 50);

		MyLabel lMap = new MyLabel("Map type", 50, 100);
		lMap.setForeColor(Color.WHITE);
		cbMapSelect = new MyComboBox(new String[]{"Highland", "Lowland", "Islands"}, 200, 100);

		MyLabel lSize = new MyLabel("Map size", 50, 150);
		lSize.setForeColor(Color.WHITE);
		cbMapSize = new MyComboBox(new String[]{"Tiny", "Small", "Medium", "Large"}, 200, 150);

		MyLabel lLimit = new MyLabel("Limit population", 50, 200);
		lLimit.setForeColor(Color.WHITE);
		cbLimit = new MyComboBox(new String[]{"No Limit", "50", "75", "100", "150", "200"}, 200, 200);

		MyLabel lStartMoney = new MyLabel("Start money", 50, 250);
		lStartMoney.setForeColor(Color.WHITE);
		cbStartMoney = new MyComboBox(new String[]{"Standard", "Low", "Medium", "High"}, 200, 250);

		cAlowCheat = new MyTextCheckBox("Alow Cheat", 50, 300);
		cAlowCheat.setChecked(true);

		custumMapPanel.add(lLevel);
		custumMapPanel.add(lMap);
		custumMapPanel.add(lSize);
		custumMapPanel.add(lLimit);
		custumMapPanel.add(lStartMoney);

		custumMapPanel.add(cbLevelSelect);
		custumMapPanel.add(cbMapSelect);
		custumMapPanel.add(cbMapSize);
		custumMapPanel.add(cbLimit);
		custumMapPanel.add(cbStartMoney);
		custumMapPanel.add(cAlowCheat);

	}

	@Override
	public void update(float t) {
		if (state == 0) {
			time--;
			custumPlayerPanel.move(CP_PANEL_SPEED, 0);
			navigationPanel.move(N_PANEL_SPEED, 0);
			custumMapPanel.move(-CM_PANEL_SPEED, 0);
			if (time <= 0) {
				time = 8;
				state = 1;
			}
		} else if (state == 2) {
			time--;
			custumPlayerPanel.move(-CP_PANEL_SPEED, 0);
			navigationPanel.move(-N_PANEL_SPEED, 0);
			custumMapPanel.move(CM_PANEL_SPEED, 0);
			if (time <= 0) {
				time = MOVE_TIME;
				state = 0;
				game.gameState = new GameMenuState(game);
			}
		}
	}

	public void ValidatePlayer(MyComboBox box) {
		if (box.getSelectedIndex() == 0) {
			int[] newIndex = new int[maxNPlayer];
			int newPlayer = -1;
			int oldPlayer = -1;

			for (int i = 0; i < maxNPlayer; i++) {
				int n = playersBox[i].getSelectedIndex();
				newIndex[i] = n;
				if (n == PLAYER && players[i] != PLAYER) {
					newPlayer = i;
				} else if (n == PLAYER && players[i] == PLAYER) {
					oldPlayer = i;
				}
			}
			players[oldPlayer] = players[newPlayer];
			playersBox[oldPlayer].setSelectedIndex(players[oldPlayer]);
			playersBox[oldPlayer].setEnable(true);
			players[newPlayer] = PLAYER;
			playersBox[newPlayer].setSelectedIndex(PLAYER);
			playersBox[newPlayer].setEnable(false);
			return;
		}
		nComTeams = 0;
		for (int i = 0; i < maxNPlayer; i++) {
			int n = playersBox[i].getSelectedIndex();
			players[i] = n;
			if (n == COMPUTER) {
				nComTeams++;
			}
		}
		if (nComTeams == 0) {
			for (int i = 0; i < maxNPlayer; i++) {
				if (players[i] == EMPTY) {
					players[i] = COMPUTER;
					playersBox[i].setSelectedIndex(COMPUTER);
					break;
				}
			}
		}
	}
}
